import { defineStore } from 'pinia'

/**
 * 用户状态管理
 * 存储用户信息、健康数据等
 */
export const useUserStore = defineStore('user', {
  state: () => ({
    // 用户基本信息
    userInfo: {
      name: '柯小畅',
      avatar: './images/avatar.jpg',
      level: 3,
      points: 2450
    },
    
    // 健康数据
    healthData: {
      weight: 65.5,
      weightHistory: [74.8, 74.1, 73.5, 72.8, 72.0, 71.2, 70.5],
      height: 172,
      bmi: 24.3,
      targetWeight: 67,
      checkInDays: 35,
      weightLoss: 4.3
    },
    
    // 成就数据
    achievements: [
      { id: 1, name: '连续打卡', icon: 'fire', level: 2, unlocked: true },
      { id: 2, name: '减重达人', icon: 'weight-scale', level: 1, unlocked: true },
      { id: 3, name: '饮食专家', icon: 'utensils', level: 1, unlocked: true },
      { id: 4, name: '运动达人', icon: 'dumbbell', level: 0, unlocked: false },
      { id: 5, name: '目标达成', icon: 'trophy', level: 0, unlocked: false }
    ]
  }),
  
  getters: {
    // 获取BMI状态
    bmiStatus: (state) => {
      const bmi = state.healthData.bmi
      if (bmi < 18.5) return '偏瘦'
      if (bmi < 24) return '正常'
      if (bmi < 28) return '偏胖'
      return '肥胖'
    },
    
    // 计算减重进度百分比
    weightLossProgress: (state) => {
      const current = state.healthData.weight
      const target = state.healthData.targetWeight
      const initial = state.healthData.weightHistory[0]
      
      if (initial <= target) return 100
      const total = initial - target
      const achieved = initial - current
      return Math.round((achieved / total) * 100)
    }
  },
  
  actions: {
    // 更新体重
    updateWeight(weight) {
      this.healthData.weight = weight
      this.healthData.weightHistory.push(weight)
      
      // 更新BMI
      const heightInMeters = this.healthData.height / 100
      this.healthData.bmi = Number((weight / (heightInMeters * heightInMeters)).toFixed(1))
      
      // 计算累计减重
      const initialWeight = this.healthData.weightHistory[0]
      this.healthData.weightLoss = Number((initialWeight - weight).toFixed(1))
    },
    
    // 增加签到天数
    increaseCheckInDays() {
      this.healthData.checkInDays++
    },
    
    // 增加积分
    addPoints(points) {
      this.userInfo.points += points
    }
  }
}) 